require("../css/mine.less");

const utils = require('../utils/utils.js');

const axios = require("../utils/axiosUtil.js");

const baseUrl = require("../utils/baseUrl.js");

window.onload = function () {
    utils.footer("mine");

    let user = document.querySelector(".user-name");
    let signs = document.querySelector(".sign span");
    let energy = document.querySelector(".energy span");
    let timer = document.querySelector(".timer span");
    let img = document.querySelector(".head-picture img");
   
    function render(){
        axios.get('/api/user/info').then(function (res) {

            let data = res.data.data;
            console.log(data);
            //用户名
            user.innerText = data.nickName;
            //如果后端返回的sign 有文本 替换页面 个性签名
            if (data.sign) {
                signs.innerText = data.sign
            }
            //时长
            timer.innerText = data.duration;
            //卡路里
            energy.innerText = data.calorie;
            //头像
            let imgUrl = baseUrl + data.imgUrl;
            img.src = imgUrl;
        })
    }
    render();

    let files = document.querySelector("#files");
    
    files.addEventListener("change",function(){
        //获取单个
        let file = files.files[0];
         // 将单个文件对象放到formData axios发送给后端
         let form = new FormData();
         //给form写入数据
         form.append("file",file);
         //吧图片发给后端（文件）
         axios.post("/api/shared/uploadPortrait1",form).then(function(res){
            let img = res.data.data.url;
            console.log(img);
            let data={
                imgUrl:img
            }

            axios.post("/api/user/changeInfo",data).then(function(){
                render();
                utils.toast(0,"头像修改成功")
            })
            
         })
    })

    //点击上传头像
    img.addEventListener("click",function(event){
        files.click();
        //阻止事件冒泡
        event.stopPropagation();
    })

    let heads = document.querySelector(".head");
    heads.addEventListener("click",function(){
        location.href= "../info.html"
    })

    //点击运动数据
    let shuju = document.querySelector(".sport-data");
    
    shuju.addEventListener("click",function(){
        location.href = "../yundong.html"
    })

    
}